/**
 * @author wendy
 * @date 2023/6/19 16:33
 */
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.Arrays;
import java.util.Properties;
import java.util.function.Consumer;

/**
 * @author wendy
 * @date 2022/11/11 17:35
 */
public class KfkConsumer {
    public static void main(String[] args) {
        //实例：一个consumer
        //1、配置项   kafka broker, key value 反序列化   groupid  数据读取 从哪个地方？
        //2、创建kafka consumer
        //3、循环： topic test 读取 topic 数据，读取  5s 读取
        Properties properties =new Properties();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.23.11:9092");
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
        //自定义 id 名字
        properties.put(ConsumerConfig.GROUP_ID_CONFIG,"niit01");
        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");


        KafkaConsumer<String,String> consumer = new KafkaConsumer<String, String>(properties);

        //订阅 topic
        consumer.subscribe(Arrays.asList("stuInfo2"));

        while (true){
            //读取 topic test 读取 topic 数据
            ConsumerRecords<String,String> consumerRecords = consumer.poll(5);
            for (ConsumerRecord<String,String> record :consumerRecords){
                System.out.println("-----------------------");
                System.out.println("key: "+record.key());
                System.out.println("value: " + record.value());
            }

        }







    }
}
